﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.BinaryOperator"/>.
    /// </summary>
    public abstract class BinaryOperatorVM : OperatorVM
    {
        #region /* FIELDS */

        protected ExpressionVM _leftVM;
        protected ExpressionVM _rightVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.BinaryOperator"/>.
        /// </summary>
        protected BinaryOperatorVM(BinaryOperator binaryOperator, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(binaryOperator, dictionary)
        {
            _leftVM = (ExpressionVM)CreateVM(binaryOperator.Left, false, dictionary);
            _rightVM = (ExpressionVM)CreateVM(binaryOperator.Right, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.BinaryOperator"/> model.
        /// </summary>
        public BinaryOperator BinaryOperator
        {
            get { return (BinaryOperator)CodeObject; }
        }

        /// <summary>
        /// The left-side <see cref="ExpressionVM"/>.
        /// </summary>
        public ExpressionVM Left
        {
            get { return _leftVM; }
        }

        /// <summary>
        /// The right-side <see cref="ExpressionVM"/>.
        /// </summary>
        public ExpressionVM Right
        {
            get { return _rightVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override Brush RenderBrush(RenderFlags flags)
        {
            // If there is an unresolved operator overload, render it as an error (or warning if in a doc comment)
            return (BinaryOperator.HiddenRef is UnresolvedRef ? (flags.HasFlag(RenderFlags.InDocComment) ? WARNING_BRUSH : ERROR_BRUSH) : OPERATOR_BRUSH);
        }

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            // Increase the indent level for any binary operator expressions that wrap, unless the parent
            // is the same operator (required for right-associative operators, such as Assignment - left
            // associative operators would only indent once anyway, due to the drawing order).
            base.Render(renderer, flags | (ParentVM == null || ParentVM.GetType() != GetType() ? RenderFlags.IncreaseIndent : 0));
        }

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (_leftVM != null)
                _leftVM.Render(renderer, passFlags | RenderFlags.IsPrefix);
            if (UseAlternativeDisplay)
                AltRenderOperator(renderer, flags);
            else
                RenderOperator(renderer, flags);
            RenderInfixComments(renderer, 0, flags | RenderFlags.PrefixSpace);
            if (_rightVM != null)
                _rightVM.Render(renderer, passFlags | RenderFlags.PrefixSpace);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            if (_leftVM != null)
                _leftVM.RenderVisible(renderer, flags);
            if (_rightVM != null)
                _rightVM.RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            if (_leftVM != null)
                _leftVM.UnRender();
            if (_rightVM != null)
                _rightVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderToolTip(renderer, flags);
            if (BinaryOperator.HiddenRef != null)
            {
                renderer.NewLine();
                CreateVM(BinaryOperator.HiddenRef).RenderToolTip(renderer, RenderFlags.SuppressLineCol);
            }
        }

        #endregion
    }
}
